{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get the number of each priority"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_prio_total(filename):\n",
    "    '''read the data trace and get the number of block in each priority'''\n",
    "    prio_total = {}\n",
    "    with open(filename, \"r\") as f:\n",
    "        lines = f.readlines()\n",
    "        for line in lines:\n",
    "            line_soup = line.split()\n",
    "            if prio_total.get(line_soup[3]) is None:\n",
    "                prio_total[line_soup[3]] = 1\n",
    "            else:\n",
    "                prio_total[line_soup[3]] += 1\n",
    "    print(prio_total)\n",
    "    return prio_total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_throughput(filepath):\n",
    "    with open(filepath, \"r\") as f:\n",
    "        client_lines = f.readlines()\n",
    "        client_stats = client_lines[-1].split(\",\")\n",
    "        total_time = int(client_stats[-1]) / 1000000 - 3 # s\n",
    "        total_bytes = int(client_stats[-4]) # B\n",
    "        return total_bytes * 8 / total_time / 1000000 # Mbps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_file(filename):\n",
    "    cols = {}\n",
    "    with open(filename, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "        for i in range(len(lines)):\n",
    "            words = lines[i].split()\n",
    "            if len(words) == 5:\n",
    "                if words[0] == 'BlockID':\n",
    "                    for word in words:\n",
    "                        cols[word] = []\n",
    "                    continue\n",
    "                else:\n",
    "                    cols['BlockID'].append(int(words[0]))\n",
    "                    cols['bct'].append(int(words[1]))\n",
    "                    cols['BlockSize'].append(int(words[2]))\n",
    "                    cols['Priority'].append(int(words[3]))\n",
    "                    cols['Deadline'].append(int(words[4]))\n",
    "            else:\n",
    "                continue\n",
    "    return pd.DataFrame(cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_client_csv(filepath):\n",
    "    client_df = pd.read_csv(filepath)\n",
    "    return client_df\n",
    "\n",
    "def get_bct_stat(client_df, prio_total):\n",
    "    avg_bct = sum(client_df[\"bct\"]) / len(client_df[\"bct\"])\n",
    "    avg_prio_bct = {}\n",
    "    priok_bct_ddl = None\n",
    "    prio_bct_complete_rate = {}\n",
    "    for prio in prio_total.keys():\n",
    "        if len(client_df[\"bct\"][client_df[\"Priority\"] == int(prio)]) <= 0:\n",
    "            continue\n",
    "        avg_prio_bct[prio] = sum(client_df[\"bct\"][client_df[\"Priority\"] == int(prio)]) / len(client_df[\"bct\"][client_df[\"Priority\"] == int(prio)])\n",
    "        priok_bct_ddl = client_df[[\"bct\", \"Deadline\"]][client_df[\"Priority\"] == int(prio)]\n",
    "\n",
    "        priok_before_ddl = 0\n",
    "        for i in range(len(priok_bct_ddl)):\n",
    "            if priok_bct_ddl.iloc[i][\"bct\"] < priok_bct_ddl.iloc[i][\"Deadline\"]:\n",
    "                priok_before_ddl += 1\n",
    "        prio_bct_complete_rate[prio] = priok_before_ddl / prio_total[prio]\n",
    "\n",
    "    print(\"avg_bct\", avg_bct)\n",
    "    print(\"avg_prio_bct\", avg_prio_bct)\n",
    "    print(\"prio_complete_rate\", prio_bct_complete_rate)\n",
    "    return (avg_bct, avg_prio_bct, prio_bct_complete_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "# prio_total = get_prio_total(\"examples/dtp_test/trace/block_trace/aitrans_block.txt\")\n",
    "prio_total = get_prio_total(\"/home/ubuntu/dtp_test_scripts/dataset/converted_data/GTA.csv\")\n",
    "for root, dirs, files in os.walk(\"examples/dtp_test/logs\"):\n",
    "    if not root.endswith(\"logs\"):\n",
    "        print(\"******\",root, \"******\")\n",
    "        for filename in files:\n",
    "            if filename.find(\"client-\") >= 0:\n",
    "                print(\"======\", filename, \"======================\")\n",
    "                client_df = parse_file(os.path.join(root, filename))\n",
    "                get_bct_stat(client_df, prio_total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_ = get_prio_total(\"/home/ubuntu/dtp_test_scripts/dataset/converted_data/GTA.csv\")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b04cb35a734fdf897bd7e15000621a11839838d9d7b75235038ed64bbfdbae2d"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
